
#include <BPainter>
#include <BGeometry>
#include <BChangeEvent>

#include "MouseConverter.h"

MouseConverter::MouseConverter(BGeometry* geometry)
{
	_geometry = geometry;
	_display = (BDisplay*)_geometry->texture();
	_pickedObject = 0;
}

bool MouseConverter::event(const BEvent& event)
{
	BControl::event(event);
	if (!_display)
		return false;
	if (event.type() == Event_Change)
	{
		_display->event(event);
	}
	return false;
}

void MouseConverter::paint(BPainter& painter)
{
	if (!_pickedObject)
		return;
	painter.setColor(1, 1, 1, 1);
	painter.drawText(200, 0, _pickedObject->name());
}
void MouseConverter::render(BRender& render)
{
	//render.setColor(1, 0, 0, 1);
	//render.setPointSize(5);
	//render.setPointSmooth(true);
	//render.drawPoint(_point);
}

bool MouseConverter::mouseEnterEvent(const BMouseEvent& mouseEvent)
{
	return false;
}
bool MouseConverter::mouseLeaveEvent(const BMouseEvent& mouseEvent)
{
	return false;
}
bool MouseConverter::mousePressEvent(const BMouseEvent& mouseEvent)
{
	if (_display)
	{
		_mouseEvent.setType(Event_MousePress);
		_mouseEvent.setButton(mouseEvent.button());
		_display->event(_mouseEvent);
		this->viewer()->fresh();
	}
	return false;
}
bool MouseConverter::mouseReleaseEvent(const BMouseEvent& mouseEvent)
{
	if (_display)
	{
		_mouseEvent.setType(Event_MouseRelease);
		_mouseEvent.setButton(mouseEvent.button());
		_display->event(_mouseEvent);
		this->viewer()->fresh();
	}
	return false;
}
bool MouseConverter::mouseMoveEvent(const BMouseEvent& mouseEvent)
{
	if (!_display)
		return false;

	_pickedObject = 0;
	_inside = false;
	BLine line = this->viewer()->line(mouseEvent.pos());
	BScene* scene = this->viewer()->scene();
	BPicker picker(line);
	if (BPickItem* item = picker.apply(scene))
	{
		_point = item->point();
		BShape* shape = item->shape();
		_pickedObject = shape;
		if (shape == _geometry)
		{
			_inside = true;
			BVectorArray* vertices = _geometry->vertices();
			BLine topLine((*vertices)[3], (*vertices)[2]);
			BLine leftLine((*vertices)[0], (*vertices)[3]);
			float quadX = leftLine.distance(_point);
			float quadY = topLine.distance(_point);
			float quadWidth = ((*vertices)[0] - (*vertices)[1]).length();
			float quadHeight = ((*vertices)[0] - (*vertices)[3]).length();
			int x = quadX / quadWidth * _display->width();
			int y = quadY / quadHeight * _display->height();

			_mouseEvent.setType(Event_MouseMove);
			_mouseEvent.setGlobalPos(BPoint(x, y));
			_display->event(_mouseEvent);
		}
	}
	this->viewer()->fresh();
	return false;
}

bool MouseConverter::mouseWheelEvent(const BMouseEvent& mouseEvent)
{
	return false;
}
bool MouseConverter::mouseClickEvent(const BMouseEvent& mouseEvent)
{
	if (_display)
	{
		_mouseEvent.setType(Event_MouseClick);
		_display->event(_mouseEvent);
	}
	return false;
}
bool MouseConverter::mouseDoubleClickEvent(const BMouseEvent& mouseEvent)
{
	if (_inside && _display)
	{
		emit("face", _display);
	}
	return false;
}

bool MouseConverter::dragEnterEvent(const BDragEvent& dragEvent)
{
	return false;
}
bool MouseConverter::dragLeaveEvent(const BDragEvent& dragEvent)
{
	return false;
}
bool MouseConverter::dragMoveEvent(const BDragEvent& dragEvent)
{
	return false;
}
bool MouseConverter::dragDropEvent(const BDragEvent& dragEvent)
{
	return false;
}

